package cn.lzk._05_tran;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception {
        //创建事务生产者
        TransactionMQProducer producer = new TransactionMQProducer("tran_producer");
        //设置nameserver地址
        producer.setNamesrvAddr("localhost:9876");
        //指定事务监听器
        producer.setTransactionListener(new MyTranListener());
        //开启生产者
        producer.start();
        //创建消息体
        Message message = new Message("tran", "hello","12", "我在源码学习java".getBytes(StandardCharsets.UTF_8));
        //发送单向消息
        producer.sendMessageInTransaction(message,null);
        //关闭
        producer.shutdown();
    }
}
